package oocis.util;

public class TimePeriods {
	Time timePoint, nowTimePoint;

	TimeInter timeLine, nowTimeLine;

	public int pOrL;// 0:point,1:line

	Time period;

	public TimePeriods() {
	}

	public TimePeriods(TimePeriods tp) {
		if (tp.pOrL == 0)
			setPeriods(tp.timePoint, tp.period);
		else
			setPeriods(tp.timeLine, tp.period);
	}

	/*
	 * Two initialize function, set the period is point or between two point.
	 */

	public void setPeriods(Time p, Time pe) {
		pOrL = 0;
		timePoint = new Time(p);
		nowTimePoint = new Time(p);
		period = new Time(pe);
	}

	public void setPeriods(TimeInter l, Time pe) {
		pOrL = 1;
		timeLine = new TimeInter(l);
		nowTimeLine = new TimeInter(l);
		period = new Time(pe);
	}

	/*
	 * Check if time in now period.
	 */

	public boolean inPeriods(Time tm) {
		if (pOrL == 0)
			return nowTimePoint.equal(tm); //TODO: improve it.
		else
			return nowTimeLine.inInter(tm);
	}

	public boolean valid() {
		if (pOrL == 0)
			return timePoint.valid();
		else
			return timeLine.valid();
	}

	public Time getPoint() {
		return nowTimePoint;
	}

	public TimeInter getLine() {
		return nowTimeLine;
	}

	public Time getPeriod() {
		return period;
	}

	public Time getDeadTime() {
		if (pOrL == 0)
			return nowTimePoint;
		else
			return nowTimeLine.getEndTime();
	}

	/*
	 * Now period go next.
	 */

	public void goNextPeriod() {
		if (pOrL == 0)
			nowTimePoint.goNextTime(period);
		else
			nowTimeLine.goNextInter(period);
	}

	/*
	 * Give the nearest time --> make the important time point.
	 */

	public Time nearestTime(Time tm) {
		if (pOrL == 0)
			return nowTimePoint.nearestTime(tm);
		else
			return nowTimeLine.nearestTime(tm);
	}
}
